<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

/**
 * @author Anh Nguyen <duyanhnguyen@vccorp.vn>
 * @copyright 2012
 */
class List_network extends MX_Controller
{

    private $_langCode;

    function __construct()
    {
        parent::__construct();
        $this->load->library('Layout');
        $this->_langCode = $this->mycommon->getLangCode();
        $this->load->model('list_network/list_network_model');
        $this->load->model('item_sim/item_sim_model');
        $this->load->model('list_group/list_group_model');
        session_start();
        unset($_SESSION['search']);
    }

    public function index($id, $page = '',$type='',$order='')
    {
        $data = array();
        $id = intval($id);
        $page = (intval($page) != 0) ? intval($page) : 1;
        $type = (intval($type) != 0) ? intval($type) : 1;
        $order = (intval($order) != 0) ? intval($order) : 1;

        if($type != 11 && $type != 10 && $type != 1) {
            $order = $type;
            $type = 1;
        }
        if($order != 2 && $order != 3 && $order != 1) {
            $type = $order;
            $order = 1;
        }
        //echo $id; die;
        
        $page = (intval($page) != 0) ? intval($page) : 1;
        $langCode = $this->mycommon->fetchLangCode($this->_langCode);
        $this->load->language('landing/landing', $this->_langCode);
        $data['lang'] = $this->lang->line('landing_lang');
        if ($id <= 0) {
            show_404();
        }
        $tmpInfo = $this->list_network_model->getListNetwork();
        $tmpInfoDetail = $this->list_network_model->getInfoNetwork($id);

        if (!$tmpInfoDetail) {
            show_404();
        } else {
            $this->layout->setMenuActive($id);
            $data['headTitle'] = $tmpInfoDetail['name'];
            $data['catInfo'] = $tmpInfo[0];

            $this->load->library('Pagination');
            $numPost = $this->item_sim_model->getCountListSimByNetworkId($id, $order, $type);

            $config['base_url'] = base_url('sim-mang-'.$this->mycommon->convertToAlias($tmpInfoDetail['name']) . '_' . $tmpInfoDetail['id']);
            $config['prefix'] = ($this->_langCode == 'vietnam') ? 'trang_' : 'page_';
            $config['suffix']= '';
            if($type == 1) {
                if($order==1) {
                    $config['suffix'] .= '';
                }
                else $config['suffix'] .='_'.$order;
                
            } else {
                if($order==1) {
                    $config['suffix'] .= '_'.$type;
                    
                }
                else $config['suffix'] .='_'.$type.'_'.$order;
            }
            $config['suffix'] .= '.html';
            $config['total_rows'] = $numPost;
            $config['per_page'] = 50;
            $config['cur_page'] = $page;
            $config['use_page_numbers'] = TRUE;
            $config['first_link'] = '«';
            $config['last_link'] = '»';
            $config['cur_tag_open'] = '<a class="active-page">';
            $config['cur_tag_close'] = '</a>';
            $config['num_links'] = 4;
            $config['uri_segment'] = 4;
            $this->pagination->initialize($config);
            $start = ($page - 1) * $config['per_page'];
            $data['pagination'] = $this->pagination->create_links();
            $tmpPost = $this->item_sim_model->getListSimByNetworkId($id, $config['per_page'], $start, $order, $type);
            $listGroup = $this->list_group_model->getListGroup();
            
            $data['type'] = $type;
            $data['order'] = $order;
            $data['page'] = $page;
            
            $data['list_network'] = $tmpInfo;
            $data['list_group'] = $listGroup;
            $data['post'] = $tmpPost;
            $data['id'] = $id;
            $data['tmpInfoDetail'] = $tmpInfoDetail;
        }
        $this->layout->view('item_sim/item_network_view', $data);
    }
    public function detail($id, $detail_id, $page = '',$type='',$order='')
    {	
    	$data = array();
    	$id = intval($id);
    	$detail_id = intval($detail_id);
    	//echo $id; die;
    	 $page = (intval($page) != 0) ? intval($page) : 1;
        $type = (intval($type) != 0) ? intval($type) : 1;
        $order = (intval($order) != 0) ? intval($order) : 1;

        if($type != 11 && $type != 10 && $type != 1) {
            $order = $type;
            $type = 1;
        }
        if($order != 2 && $order != 3 && $order != 1) {
            $type = $order;
            $order = 1;
        }

        $page = (intval($page) != 0) ? intval($page) : 1;
    	$langCode = $this->mycommon->fetchLangCode($this->_langCode);
    	$this->load->language('landing/landing', $this->_langCode);
    	$data['lang'] = $this->lang->line('landing_lang');
    	
    	if ($id <= 0) {
    		show_404();
    	}
    	$tmpInfo = $this->list_network_model->getListNetwork($id, $langCode);
        $tmpInfoDetail = $this->list_network_model->getInfoNetwork($id);
        $tmpDetail = $this->list_group_model->getDetail($detail_id);

    	if (!$tmpDetail) {
            show_404();
        } else {
    		$this->layout->setMenuActive($id);
    		$data['headTitle'] = $tmpInfoDetail['name']. '-' . $tmpDetail['name'];
    		$data['catInfo'] = $tmpInfoDetail;
    		$this->load->model('post/post_model');
    		$this->load->library('Pagination');
    		$numPost = $this->item_sim_model->getCountListSimByNetworkIdAndGroupId($id, $detail_id, $order, $type);
            
    		$config['base_url'] = base_url('mang/' . $this->mycommon->convertToAlias($tmpInfoDetail['name']) . '_' . $id.'/nhom_'.$this->mycommon->convertToAlias($tmpDetail['name']).'_'.$detail_id);
    		$config['prefix'] = ($this->_langCode == 'vietnam') ? 'trang_' : 'page_';
    		$config['suffix']= '';
            if($type == 1) {
                if($order==1) {
                    $config['suffix'] .= '';
                }
                else $config['suffix'] .='_'.$order;
                
            } else {
                if($order==1) {
                    $config['suffix'] .= '_'.$type;
                    
                }
                else $config['suffix'] .='_'.$type.'_'.$order;
            }
            $config['suffix'] .= '.html';
    		$config['total_rows'] = $numPost;
    		$config['per_page'] = 50;
    		$config['cur_page'] = $page;
    		$config['use_page_numbers'] = TRUE;
    		$config['first_link'] = '«';
    		$config['last_link'] = '»';
    		$config['cur_tag_open'] = '<a class="active-page">';
    		$config['cur_tag_close'] = '</a>';
    		$config['num_links'] = 4;
    		$config['uri_segment'] = 5;
    		$this->pagination->initialize($config);
    		$start = ($page - 1) * $config['per_page'];
    		$data['pagination'] = $this->pagination->create_links();
    		$tmpPost = $this->item_sim_model->getListSimByNetworkIdAndGroupId($id, $detail_id, $config['per_page'], $start, $order, $type);

    		$data['post'] = $tmpPost;
    		$data['id'] = $id;
    		
            $data['type'] = $type;
            $data['order'] = $order;
            $data['page'] = $page;
            
    		$data['detail_id'] = $detail_id;
            $data['tmpInfoDetail'] = $tmpInfoDetail;
            $data['tmpDetail'] = $tmpDetail;
    	}
    	$this->layout->view('item_sim/item_network_group_view', $data);
    }
    public function export($page='')
    {
    	//echo $_SERVER['DOCUMENT_ROOT'].'/app/libraries/dompdf/dompdf_config.inc.php'; die;
    	$data = array();
    	if(isset($_GET["page"])) {
    		$page = $_GET["page"];
    	}
    	if(isset($_GET["id"])) {
    		$id = $_GET["id"];
    	}
    	if(isset($_GET["order"])) {
            $order = $_GET["order"];
        }
        if(isset($_GET["type"])) {
            $type = $_GET["type"];
        }
    	$page = (intval($page) != 0) ? intval($page) : 1;
        if($type != 11 && $type != 10 && $type != 1) {
            $order = $type;
            $type = 1;
        }
        if($order != 2 && $order != 3 && $order != 1) {
            $type = $order;
            $order = 1;
        }
    	 
    	$tmpInfo = $this->list_network_model->getListNetwork();
        $tmpInfoDetail = $this->list_network_model->getInfoNetwork($id);
 
        if (!$tmpInfoDetail) {
            show_404();
        } else {
            $this->layout->setMenuActive($id);
            $data['headTitle'] = $tmpInfo[0]['name'];
            $data['catInfo'] = $tmpInfo[0];

            $this->load->library('Pagination');
            $numPost = $this->item_sim_model->getCountListSimByNetworkId($id);
            $config['base_url'] = base_url('sim-mang-'.$this->mycommon->convertToAlias($tmpInfoDetail['name']) . '_' . $tmpInfoDetail['id']);
            $config['prefix'] = ($this->_langCode == 'vietnam') ? 'trang_' : 'page_';
            $config['suffix'] = '.html';
            $config['total_rows'] = $numPost;
            $config['per_page'] = 50;
            $config['cur_page'] = $page;
            $config['use_page_numbers'] = TRUE;
            $config['first_link'] = '«';
            $config['last_link'] = '»';
            $config['cur_tag_open'] = '<a class="active-page">';
            $config['cur_tag_close'] = '</a>';
            $config['num_links'] = 4;
            $config['uri_segment'] = 3;
            $this->pagination->initialize($config);
            $start = ($page - 1) * $config['per_page'];
            $data['pagination'] = $this->pagination->create_links();
           // $tmpPost = $this->item_sim_model->getListSimByNetworkId($id, $config['per_page'], $start, $langCode);
            $tmpPost = $this->item_sim_model->getListSimByNetworkId($id, $config['per_page'], $start, $order, $type);
            $listGroup = $this->list_group_model->getListGroup();
            
            $data['list_network'] = $tmpInfo;
            $data['list_group'] = $listGroup;
            $data['post'] = $tmpPost;
            $data['id'] = $id;
            //export
            $html = $this->load->view('item_sim/export_view', $data,true);
            $html = mb_convert_encoding($html, "iso-8859-2", "utf8");
            
            $path = getcwd()."/dompdf/dompdf_config.inc.php";
            require_once($path);
            spl_autoload_register('DOMPDF_autoload');
            $dompdf = new DOMPDF();
            $dompdf->load_html($html);
            $dompdf->render();
            $dompdf->stream("my_pdf.pdf", array("Attachment" => 0));
        }
    }
    public function export_detail($id, $detail_id, $page = '')
    {
    	$data = array();
    	if(isset($_GET["page"])) {
    		$page = $_GET["page"];
    	}
    	if(isset($_GET["id"])) {
    		$id = $_GET["id"];
    	}
    	if(isset($_GET["detail_id"])) {
    		$detail_id = $_GET["detail_id"];
    	}
        if(isset($_GET["order"])) {
            $order = $_GET["order"];
        }
        if(isset($_GET["type"])) {
            $type = $_GET["type"];
        }
    	//echo $id; die;
    	$page = (intval($page) != 0) ? intval($page) : 1;
    	$langCode = $this->mycommon->fetchLangCode($this->_langCode);
    	$this->load->language('landing/landing', $this->_langCode);
    	$data['lang'] = $this->lang->line('landing_lang');
    	 
    	if ($id <= 0) {
    		show_404();
    	}
    	$tmpInfo = $this->list_network_model->getListNetwork($id, $langCode);
    	if($tmpInfo) {
    		$this->layout->setMenuActive($id);
    		$data['headTitle'] = $tmpInfo[0]['name'];
    		$data['catInfo'] = $tmpInfo[0];
    		$this->load->model('post/post_model');
    		$this->load->library('Pagination');
    		$numPost = $this->item_sim_model->getCountListSimByNetworkIdAndGroupId($id, $detail_id, $order, $type);
    		$config['base_url'] = base_url('chuyen-muc/' . $this->mycommon->convertToAlias($tmpInfo[0]['name']) . '_' . $tmpInfo[0]['id']);
    		$config['prefix'] = ($this->_langCode == 'vietnam') ? 'trang_' : 'page_';
    		$config['suffix'] = '.html';
    		$config['total_rows'] = $numPost;
    		$config['per_page'] = 50;
    		$config['cur_page'] = $page;
    		$config['use_page_numbers'] = TRUE;
    		$config['first_link'] = '«';
    		$config['last_link'] = '»';
    		$config['cur_tag_open'] = '<a class="active-page">';
    		$config['cur_tag_close'] = '</a>';
    		$config['num_links'] = 4;
    		$config['uri_segment'] = 4;
    		$this->pagination->initialize($config);
    		$start = ($page - 1) * $config['per_page'];
    		$data['pagination'] = $this->pagination->create_links();
    		
            $tmpPost = $this->item_sim_model->getListSimByNetworkIdAndGroupId($id, $detail_id, $config['per_page'], $start, $order, $type);
    		$data['page'] = $page;
    		$data['post'] = $tmpPost;
    		$data['id'] = $id;
    		$data['type'] =2;
    		
    		//export
    		$html = $this->load->view('item_sim/export_view', $data,true);
    		$html = mb_convert_encoding($html, "iso-8859-2", "utf8");
    		
    		$path = getcwd()."/dompdf/dompdf_config.inc.php";
    		require_once($path);
    		spl_autoload_register('DOMPDF_autoload');
    		$dompdf = new DOMPDF();
    		$dompdf->load_html($html);
    		$dompdf->render();
    		$dompdf->stream("my_pdf.pdf", array("Attachment" => 0));
    	}

    }

}

?>
